package net.sxnic.workflow.whimpl;

import java.util.Map;

import net.sxnic.workflow.context.WfContextImpl;
import net.sxnic.workflow.handler.AbstractHandler;
import net.sxnic.workflow.handler.HandlerExt;
import net.sxnic.workflow.handler.WfHandlerException;
import net.sxnic.workflow.node.Node;
import net.sxnic.workflow.task.util.TaskUtils;
import net.sxnic.workflow.utils.WfManagerUtils;
import net.sxnic.workflow.wfentity.WfEntity;

public class CreateNextNodeTaskHandler extends AbstractHandler<WfEntity> implements HandlerExt<WfEntity> {

	public CreateNextNodeTaskHandler(WfContextImpl<WfEntity> context) {
		super(context);
	}

	@Override
	public void editExecute() throws WfHandlerException {

	}

	@Override
	public void saveExecute() throws WfHandlerException {

	}

	@Override
	public void viewExecute() throws WfHandlerException {

	}

	@Override
	public void submitExecute() throws WfHandlerException {
		// 根据nextAssignees为后续节点创建任务
		Map<String, String[]> map = TaskUtils.treatNextAssigneesToMap(context.getNextAssignees());

		if (map == null || map.size() == 0) {
			return;
		} else if (map.size() >= 2) {
			map = TaskUtils.sortNodes(map);
		}

		for (String nodeName : map.keySet()) {
			Node nextNode = WfManagerUtils.getNodeManager().findByPdName(context.getProcessName(), nodeName);
			for (String u : map.get(nodeName)) {
				context.getTaskManager().createTask(nextNode, u, context.getTask());
			}
		}

	}

}
